package gnippets.domain

import refcode.RefCode

/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/**
 *  <p>Integration Tests for the User class</p>
 *
 *  @author <a href='mailto:kouphax@gmail.com'>James Hughes</a>
 */
class GnippetsUserTests extends GroovyTestCase {
    
    
    //=========================================================================
    //
    //                    U N I T   T E S T   S E T   U P
    //
    //=========================================================================
    void setUp(){
    
        /* create a predefined user */
        def predefinedUser = new GnippetsUser(
            username:'predefined',
            password:'password',
            realName:'Test User',
            email:'predefined@unittest.com'        
        )
        
        /* save predefined user */
        if(!predefinedUser.save()){
            throw new Exception("Error in setup method. Save user failed.");
        }
    }
    
    //=========================================================================
    //
    //          U S E R N A M E   C O N S T R A I N T S   T E S T S
    //
    //=========================================================================
    void testConstraintsUsernameNull() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            password:'password',
            realName:'Test User',
            email:'user@unittest.com'            
        )
        
        /* FAIL */
        assert testUser.validate() == false
        
        /* fix the problem */
        testUser.username = 'username'
        
        /* PASS */
        assert testUser.validate() == true
    }      
    
    void testConstraintsUsernameBlank() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            username:'',
            password:'password',
            realName:'Test User',
            email:'user@unittest.com'            
        )
        
        /* FAIL */
        assert testUser.validate() == false
        
        /* fix the problem */
        testUser.username = 'username'
        
        /* PASS */
        assert testUser.validate() == true
    }    
    
    void testConstraintsUsernameUnique() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            username:'predefined',
            password:'password',
            realName:'Test User',
            email:'user@unittest.com'            
        )
        
        /* FAIL */
        assert testUser.validate() == false
        
        /* fix the problem */
        testUser.username = 'username'
        
        /* PASS */
        assert testUser.validate() == true
    }          
    
    
    //=========================================================================
    //
    //            P A S S W O R D  C O N S T R A I N T S   T E S T S
    //
    //=========================================================================    
    void testConstraintsPasswordSizeTooSmall() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            username:'testuser',
            password:'1234',
            realName:'Test User',
            email:'user@unittest.com'            
        )
        
        /* FAIL */
        assert testUser.validate() == false
        
        /* fix the problem */
        testUser.password = '12345'
        
        /* PASS */
        assert testUser.validate() == true
    }      
    
    void testConstraintsPasswordBlank() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            username:'testuser',
            password:'',
            realName:'Test User',
            email:'user@unittest.com'            
        )
        
        /* FAIL */
        assert testUser.validate() == false
        
        /* fix the problem */
        testUser.password = 'password'
        
        /* PASS */
        assert testUser.validate() == true
    }    
    
    void testConstraintsPasswordNull() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            username:'testuser',
            realName:'Test User',
            email:'user@unittest.com'            
        )
        
        /* FAIL */
        assert testUser.validate() == false
        
        /* fix the problem */
        testUser.password = 'password'
        
        /* PASS */
        assert testUser.validate() == true
    }        
    
    //=========================================================================
    //
    //          R E A L   N A M E  C O N S T R A I N T S   T E S T S
    //
    //========================================================================= 

    void testConstraintsRealNameAllowNull() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            username:'username',
            password:'password',
            email:'user@unittest.com'            
        )
        
        /* PASS */
        assert testUser.validate() == true
    }      
    
    void testConstraintsRealNameAllowBlank() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            username:'username',
            password:'password',
            realName:'',
            email:'user@unittest.com'            
        )
        
        /* FAIL */
        assert testUser.validate() == true
    }      
    
    //=========================================================================
    //
    //                E M A I L  C O N S T R A I N T S   T E S T S
    //
    //=========================================================================     
    
    void testConstraintsEmailNull() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            username:'testuser',
            password:'password',
            realName:'Test User'
        )
        
        /* FAIL */
        assert testUser.validate() == false
        
        /* fix the problem */
        testUser.email = 'testuser@test.com'
        
        /* PASS */
        assert testUser.validate() == true
    }   
    
    void testConstraintsEmailFormat() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            username:'testuser',
            password:'password',
            realName:'Test User',
            email:'invaliddemail.com'
        )
        
        /* FAIL */
        assert testUser.validate() == false
        
        /* fix the problem */
        testUser.email = 'testuser@test.com'
        
        /* PASS */
        assert testUser.validate() == true
    }           
    
    void testConstraintsEmailBlank() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            username:'testuser',
            password:'password',
            realName:'Test User',
            email:''
        )
        
        /* FAIL */
        assert testUser.validate() == false
        
        /* fix the problem */
        testUser.email = 'testuser@test.com'
        
        /* PASS */
        assert testUser.validate() == true
    }     
    
    void testConstraintsEmailUnique() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            username:'testuser',
            password:'password',
            realName:'Test User',
            email:'predefined@unittest.com'
        )
        
        /* FAIL */
        assert testUser.validate() == false
        
        /* fix the problem */
        testUser.email = 'testuser@test.com'
        
        /* PASS */
        assert testUser.validate() == true
    }       
    
    //=========================================================================
    //
    //                 U R L  C O N S T R A I N T S   T E S T S
    //
    //=========================================================================      
    
    void testConstraintsUrlNull() {
    
        /* construct user */
        def testUser = new GnippetsUser(
            username:'testuser',
            password:'password',
            realName:'Test User',
            email:'testuser@test.com'
        )
        
        /* PASS */
        assert testUser.validate() == true
        
        testUser.url = new URL('http://www.test.com')
        
        /* PASS */
        assert testUser.validate() == true
    }         
}


